package android.test.devicetest;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.test.getintent.GetIntent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.test.until.Max3HWVerion;

public class Volume extends Activity {
    private static final int EXT_SPK_DET_INTV = 1000;
    private AudioManager mAudioManager;
    private MediaPlayer mPlayer;
    private int currentVolume;
    private int maxVolume;
    private TextView displayVolme;
    private TextView externalSpk;
    private int num = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.volume);
        try {
            playMedia();
            mPlayer.setLooping(true);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

//      mTimerHandler.postDelayed(mTimerRunnable, EXT_SPK_DET_INTV);
    }

    private void playMedia() throws IOException {
        // TODO Auto-generated method stub

        displayVolme = (TextView) findViewById(R.id.disp_volme);
        externalSpk = (TextView) findViewById(R.id.ext_spk);
        mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        displayVolme.setText(String.valueOf("Volume:" + currentVolume));
        externalSpk.setVisibility(View.GONE);

        mPlayer = MediaPlayer.create(this, R.raw.volume);
        try {
            // mPlayer.prepareAsync();
            mPlayer.start();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        mPlayer.release();
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            currentVolume++;
            if (currentVolume > maxVolume) {
                currentVolume = maxVolume;
            }
            break;

        case KeyEvent.KEYCODE_DPAD_LEFT:
            currentVolume--;
            if (currentVolume < 0) {
                currentVolume = 0;
            }
            break;
        }

        displayVolme.setText(String.valueOf("Volume:" + currentVolume));
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        switch (keyCode) {
        case KeyEvent.KEYCODE_ENTER:
            if (num == 1)
            {
                mPlayer.release();
                mPlayer = MediaPlayer.create(this, R.raw.test);
                mPlayer.start();
                mPlayer.setLooping(true);
            }
            if (num == 2)
            {
//              mTimerHandler.removeCallbacks(mTimerRunnable);
                mPlayer.release();
                finish();
                getIntents("video");
            }
            num++;
            break;

        case KeyEvent.KEYCODE_BACK:
//          mTimerHandler.removeCallbacks(mTimerRunnable);
            mPlayer.release();
            finish();
            System.exit(0);
            break;
        }
        return super.onKeyUp(keyCode, event);
    }

    private void getIntents(String intent) {
        Intent mIntent = GetIntent.getItemIntent(this, intent);
        if (mIntent != null) {
            startActivity(mIntent);
        }
    }

    Handler mTimerHandler = new Handler();
    Runnable mTimerRunnable = new Runnable() {
        @Override
        public void run() {
            mTimerHandler.postDelayed(mTimerRunnable, EXT_SPK_DET_INTV);
            externalSpk.setText("externel speaker: " + Max3HWVerion.getExternelSpeaker());
        }
    };
}
